import Vue from "vue";
import Router from "vue-router";
import {GetQueryByString} from '@/utils'
import config from "@/api/config";

const originalPush = Router.prototype.push;
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
};

Vue.use(Router);

const router = new Router({
    scrollBehavior (to, from, savedPosition) {
        if (savedPosition) {
          return savedPosition
        } else {
          return { x: 0, y: 0 }
        }
    },
    mode: 'history',
    routes: [
        { path: "/",redirect: '/login' },//redirect: '/loading'
        { path: '/loading',name: 'loading',component: resolve => require(['@/views/admin/Loading'], resolve)},
        { path: '/login', name: 'login',component: resolve => require(['@/views/admin/Login'], resolve)},
        { path: "/index", name: "index",meta: {keepAlive: true},component: resolve => require(['@/views/admin/Index'], resolve) },
        { path: "/deviceManage", name: "deviceManage",component: resolve => require(['@/views/admin/DeviceManage'], resolve) },
        { path: "/entrance", name: "entrance",component: resolve => require(['@/components/admin/entrance'], resolve) },
        { path: "/exit", name: "exit",component: resolve => require(['@/components/admin/exit'], resolve) },
        { path: "/carManage",name: "carManage",component: resolve => require(['@/views/admin/CarManage'], resolve) },
        { path: "/presentCar",name: "presentCar",component: resolve => require(['@/views/admin/PresentCar'], resolve) },
        { path: "/detentionInventory", name: "detentionInventory",component: resolve => require(['@/views/admin/DetentionInventory'], resolve) },
        { path: "/blacklist",name: "blacklist",component: resolve => require(['@/views/admin/Blacklist'], resolve) },
        { path: "/blankpage", name: "blankpage",component: resolve => require(['@/views/admin/Blankpage'], resolve) },
        { path: "/statistical",name: "statistical",component: resolve => require(['@/views/admin/Statistical'], resolve) },
        { path: "/addCar",name: "addCar",component: resolve => require(['@/components/admin/addCar'], resolve) },
        { path:"/order",name: "order",component: resolve => require(['@/views/admin/Order'], resolve) },
        { path:"/refund",name: "refund",component: resolve => require(['@/views/admin/Refund'], resolve) },
        { path:"/detail",name: "detail",component: resolve => require(['@/views/admin/Detail'], resolve) },
        { path:"/inOut",name: "inOut",component: resolve => require(['@/views/admin/InOut'], resolve) },
        { path:"/payRecords",name: "payRecords",component: resolve => require(['@/views/admin/PayRecords'], resolve) },
        { path:"/member",name: "member",component: resolve => require(['@/views/admin/Member'], resolve) },
        { path:"/examine",name: "examine",component: resolve => require(['@/views/admin/Examine'], resolve) },
        { path:"/detailExamine",name: "detailExamine",component: resolve => require(['@/views/admin/detailExamine'], resolve) },
        { path:"/showDetailExamine",name: "showDetailExamine",component: resolve => require(['@/views/admin/showDetailExamine'], resolve) },
        { path:"/setMeal",name: "setMeal",component: resolve => require(['@/views/admin/member/setMeal'], resolve) },
        { path:"/user", name: "user",component: resolve => require(['@/views/admin/member/user'], resolve) },
        { path:"/consume",name: "consume",component: resolve => require(['@/views/admin/member/consume'], resolve) },
        { path:"/recharge",name: "recharge",component: resolve => require(['@/views/admin/member/recharge'], resolve) },
        { path:"/dues", name: "dues", component: resolve => require(['@/views/admin/member/dues'], resolve) },
        { path:"/project",name: "project",component: resolve => require(['@/views/admin/member/project'], resolve) },
        //商户路由
        { path: '/loadingShop', name: 'loadingShop', component: resolve => require(['@/views/business/LoadingShop'], resolve) },
        { path: "/blankpageShop",name: "blankpageShop",component: resolve => require(['@/views/business/BlankpageShop'], resolve) },        
        { path: '/loginShop',name: 'loginShop',component: resolve => require(['@/views/business/LoginShop'], resolve) },
        { path: "/indexShop",name: "indexShop",meta: {keepAlive: true}, component: resolve => require(['@/views/business/IndexShop'], resolve) },
        { path:"/orderShop", name: "orderShop",component: resolve => require(['@/views/business/OrderShop'], resolve) },
        { path:"/couponsIndex", name: "couponsIndex",component: resolve => require(['@/views/business/coupons/index'], resolve) },
        { path:"/issueCoupons", name: "issueCoupons",component: resolve => require(['@/views/business/coupons/issueCoupons'], resolve) },
        { path:"/stock", name: "stock",component: resolve => require(['@/views/business/coupons/stock'], resolve) },
        { path:"/stockDetail", name: "stockDetail",component: resolve => require(['@/views/business/coupons/stockDetail'], resolve) },
        { path:"/rechargeRecord", name: "rechargeRecord",component: resolve => require(['@/views/business/coupons/rechargeRecord'], resolve) },
        { path:"/grant", name: "grant",component: resolve => require(['@/views/business/coupons/grant'], resolve) },
        { path:"/usageRecord", name: "usageRecord",component: resolve => require(['@/views/business/coupons/usageRecord'], resolve) },
        { path:"/returnRecord", name: "returnRecord",component: resolve => require(['@/views/business/coupons/returnRecord'], resolve) },
        { path:"/inventoryRecharge", name: "inventoryRecharge",component: resolve => require(['@/views/business/coupons/inventoryRecharge'], resolve) },
        { path:"/batch", name: "batch",component: resolve => require(['@/views/business/coupons/batch'], resolve) },
        { path:"/banquetCoupons", name: "banquetCoupons",component: resolve => require(['@/views/business/coupons/banquetCoupons'], resolve) },
        { path:"/dynamicCoupons", name: "dynamicCoupons",meta: {keepAlive: true},component: resolve => require(['@/views/business/coupons/dynamicCoupons'], resolve)  },
        { path:"/showQrCode", name: "showQrCode",component: resolve => require(['@/views/business/coupons/showQrCode'], resolve) },
        // 车主路由
        { path: '/loadingOwner',name: 'loadingOwner',component: resolve => require(['@/views/owner/LoadingOwner'], resolve) },
        { path: "/blankpageOwner",name: "blankpageOwner",component: resolve => require(['@/views/owner/BlankpageOwner'], resolve) },        
        { path: '/loginOwner',name: 'loginOwner',component: resolve => require(['@/views/owner/LoginOwner'], resolve) },
        { path: "/indexOwner",name: "indexOwner",component: resolve => require(['@/views/owner/IndexOwner'], resolve) },
        { path: "/myCar",name: "myCar",component: resolve => require(['@/views/owner/myCar'], resolve) },
        { path: "/payment",name: "payment",component: resolve => require(['@/views/owner/payment'], resolve) },
        { path: "/carPark",name: "carPark",component: resolve => require(['@/views/owner/carPark'], resolve) },
        { path: "/bill",name: "bill",component: resolve => require(['@/views/owner/bill'], resolve) },
        { path: "/monthlyCar",name: "monthlyCar",component: resolve => require(['@/views/owner/monthlyCar'], resolve) },
        { path: "/renew",name: "renew",component: resolve => require(['@/views/owner/renew'], resolve) },
        { path: "/invoice",name: "invoice",component: resolve => require(['@/views/owner/invoice'], resolve) },
        { path: "/afterPayment",name: "afterPayment",component: resolve => require(['@/views/owner/afterPayment'], resolve) },
        { path: "/memberRecharge",name: "memberRecharge",component: resolve => require(['@/views/owner/member'], resolve) },
        { path: "/rechargePackage",name: "rechargePackage",component: resolve => require(['@/views/owner/rechargePackage'], resolve) },
        { path: "/temporaryStop", name: "temporaryStop",component: resolve => require(['@/views/owner/temporaryStop'], resolve) },
        { path: "/payTasaday", name: "payTasaday", component: resolve => require(['@/views/owner/payTasaday'], resolve) },
        { path: "/advert",name: "advert",component: resolve => require(['@/views/owner/advert'], resolve) },
        { path: "/contactUs", name: "contactUs",component: resolve => require(['@/views/owner/contactUs'], resolve) }
    ]
});

export default router

router.beforeEach((to, from, next) => { 
    let isURL = window.location.href.indexOf('code=') === -1
    if((isURL&&to.path!=='/monthlyCar')){   
        wxLogin(to.path)  
    } 
    let isIdURL = window.location.href.indexOf('id=') === -1
    let isAppIdURL = window.location.href.indexOf('appId=') === -1
    if((to.path==='/showDetailExamine'&&!isIdURL)||(to.path==='/monthlyCar'&&!isAppIdURL)){
        debugger
        wxLogin(to.path)
    }
    //debugger
    /* if(!token){  
        const url=window.location.href;//获取当前地址栏
        //const openid=GetQueryByString(url,'openid');//GetQueryByString 自己封装的方法来获取地址栏的参数
        //alert('3')
        if(isURL){
            //alert('4')
            //wxLogin(to.path)
        }
    } */
    if (to.path === '/login'||to.path === '/loginShop'||to.path === '/loginOwner'
        ||to.path === '/'||to.path === '/loading'||to.path === '/loadingShop'||to.path === '/loadingOwner') {
        sessionStorage.clear()
        next();
    } else {        
        let token = sessionStorage.getItem("token"); 
        if(to.path.indexOf('Shop')>-1){
            if (token === 'null' || token === '') {
                next('/loginShop');
            } else {
                next();
            }
        }else if(to.path.indexOf('Owner')==-1){  /* to.path === '/indexShop' */
            if (token === 'null' || token === '') {
                next('/loginOwner');
            } else {
                next();
            }
        }else{
            if (token === 'null' || token === '') {
                next('/login');
            } else {
                next();
            }
        }
    }
    next()
})

function wxLogin(path){
    console.log(path,"path");
    let snsapiScope = "snsapi_base" 
    const wxLoginJump = (appid, url,snsapi_scope) => {
        let redirect_uri = encodeURIComponent(url)              
        //this.$toast(redirect_uri);
        window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${redirect_uri}&response_type=code&scope=${snsapi_scope}&state=STATUS#wechat_redirect`
    }
    if(path == '/loading'){        
        wxLoginJump(config.appID, config.pageUrl+'/Blankpage?appId='+config.appID,snsapiScope)
    }else if(path == '/loadingShop'){
        wxLoginJump(config.appID, config.pageUrl+'/BlankpageShop?appId='+config.appID,snsapiScope)
    }else if(path == '/loadingOwner'){
        snsapiScope="snsapi_userinfo"
        wxLoginJump(config.appID, config.pageUrl+'/BlankpageOwner?appId='+config.appID,snsapiScope)
    }else if(path==='/showDetailExamine'){
        const url=window.location.href;//获取当前地址栏
        const examineId=GetQueryByString(url,'id')
        const appId = GetQueryByString(url,'appId')
        console.log(appId,'appId')
        console.log(examineId,'examineId')
        wxLoginJump(appId, config.pageUrl+'/showDetailExamine?appId='+appId+'&examineId='+examineId,snsapiScope)
    }else if(path==='/monthlyCar'){
        debugger
        /* const url=window.location.href;//获取当前地址栏
        const userId=GetQueryByString(url,'userId')
        const appId = GetQueryByString(url,'appId')
        wxLoginJump(appId, config.pageUrl+'/monthlyCar?appId='+appId+'&userId='+userId,snsapiScope) */
    }
}